package com.wzw.ad;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

public class ADServlet extends HttpServlet {

	private static final long serialVersionUID = 6589817983670821584L;
	private static final String OP_ADD_AD = "operation.add.ad";
	private static final String OP_EDIT = "operation.edit";
	private static final String OP_DELETE = "operation.delete";
	private static final String OP_ADD_MEDIA = "operation.add.media";
	private static final String OP_GET_MEDIALIST = "operation.getMediaList";
	private static final String OP_GET_ADLIST = "operation.getADList";
	
	private static final String RETURN_SUCCESS = "success";
	private static final String RETURN_FAILED = "failed";

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String op = req.getParameter("op");
		String result = "";
		if(OP_ADD_AD.equalsIgnoreCase(op)) {
			result = addAD(req, resp);
		} else if(OP_ADD_MEDIA.equalsIgnoreCase(op)) {
			result = addMedia(req, resp);
		} else if(OP_EDIT.equalsIgnoreCase(op)) {
			result = editAD(req, resp);
		} else if(OP_DELETE.equalsIgnoreCase(op)) {
			result = deleteAD(req, resp);
		} else if(OP_GET_MEDIALIST.equalsIgnoreCase(op)) {
			result = getMediaList(req, resp);
		} else if(OP_GET_ADLIST.equalsIgnoreCase(op)) {
			result = getADInfo(req, resp);
		}
		resp.getWriter().write(result);
	}
	
	private String addAD(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String projectCode = req.getParameter("projectCode");
		String mediaId = req.getParameter("mediaId");
		String ADName = req.getParameter("ADName");
//		String adUrl = req.getParameter("adUrl");
		Adposition ad = new Adposition();
		ad.setMediaId(mediaId);
		ad.setADName(ADName);
//		ad.setAdUrl(adUrl);
		try {
			if(ADTools.addAD(ad, projectCode)) {
				return RETURN_SUCCESS;
			} else {
				return RETURN_FAILED;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private String addMedia(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String projectCode = req.getParameter("projectCode");
		String mediaName = req.getParameter("mediaName");
		Adposition ad = new Adposition();
		ad.setMediaName(mediaName);
		try {
			if(ADTools.addMedia(ad, projectCode)) {
				return RETURN_SUCCESS;
			} else {
				return RETURN_FAILED;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private String editAD(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String projectCode = req.getParameter("projectCode");
		String ADId = req.getParameter("ADId");
//		String mediaId = req.getParameter("mediaId");
		String ADName = req.getParameter("ADName");
//		String mediaName = req.getParameter("mediaName");
//		String adUrl = req.getParameter("adUrl");
//		String hitCount = req.getParameter("hitCount");
//		String registerCount = req.getParameter("registerCount");
		Adposition ad = new Adposition();
		ad.setADId(ADId);
//		ad.setMediaId(mediaId);
		ad.setADName(ADName);
//		ad.setMediaName(mediaName);
//		ad.setAdUrl(adUrl);
//		ad.setHitCount(hitCount);
//		ad.setRegisterCount(registerCount);
		try {
			if(ADTools.editAD(ad, projectCode)) {
				return RETURN_SUCCESS;
			} else {
				return RETURN_FAILED;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private String deleteAD(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String adId = req.getParameter("adId");
		try {
			if(ADTools.deleteAD(adId)) {
				return RETURN_SUCCESS;
			} else {
				return RETURN_FAILED;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private String getMediaList(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<Adposition> mList = ADTools.getMediaList(null);
		JSONArray json = JSONArray.fromObject(mList);
		return json.toString();
	}
	
	private String getADInfo(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String ADId = req.getParameter("ADId");
		Adposition ad = ADTools.getADInfo(ADId, null);
		return ad.toJSONString();
	}

}
